const region = require('./region.js')
const util = require("./util.js")

const form = {
  id: "",
  random_code: "",
  baseUrl: util.baseUrl,
  task: {
    id: "",
    projectName: "",
    communityName: "",
  },
  curYear: (new Date()).getFullYear(),
  customData: {},
  customRender: {},
  customList: [],
  itemData: {}, // 保存form的数据
  itemRender: {}, // 保存页面加载的时候需要渲染的数据结构
  itemList: [], // 控件列表

  // 加载页面渲染所需数据
  loadData: (responseData) => {
    if (typeof responseData === "object") {
      form.task = responseData.task
      form.customData = responseData.customData
      form.customRender = util.clone(responseData.customData)
      form.customList = responseData.customItemList

      form.itemData = responseData.itemData
      form.itemRender = util.clone(responseData.itemData)
      form.itemList = responseData.itemList
      form.random_code = responseData.itemData.random_code || ""
      
      // itemRender 对于特殊需要渲染支持的数据进行处理
      for (var item in form.itemList) {
        var name = form.itemList[item]['name']
        if (name && form.itemRender[name] !== undefined) {
          // 三级联动
          if (form.itemList[item].responseType.id == "91") {
            let valueStr = form.itemRender[name]
            let arrData = valueStr.split(",")
            form.itemRender[name] = region.getRegionByCode(arrData[0], arrData[1], arrData[2])
            form.itemRender[name]["text"] = arrData[3]
            // 图片组件
          } else if (form.itemList[item].responseType.id == "9") {
            let valueStr = form.itemRender[name] || ""
            if (!valueStr) {
              form.itemRender[name] = []
            } else {
              let arrData = valueStr.split(",")
              for (var item in arrData) {
                arrData[item] = [util.baseUrl, arrData[item]]
              }
              form.itemRender[name] = arrData
            }
            // 复选框组件
          } else if (form.itemList[item].responseType.id == "3") {
            let valueStr = form.itemRender[name] || ""
            if (!valueStr) {
              form.itemRender[name] = {}
              for (var i in form.itemList[item].responseSet.options) {
                form.itemRender[name][form.itemList[item].responseSet.options[i].value] = false;
              }
            } else {
              form.itemRender[name] = {}
              for (var i in form.itemList[item].responseSet.options) {
                if (valueStr.indexOf(form.itemList[item].responseSet.options[i].value) != -1) {
                  form.itemRender[name][form.itemList[item].responseSet.options[i].value] = true;
                } else {
                  form.itemRender[name][form.itemList[item].responseSet.options[i].value] = false;
                }
              }
            }
            // 单选组件
          } else if (form.itemList[item].responseType.id == "5") {
            let valueStr = form.itemRender[name] || ""
            let sIndex = valueStr.indexOf('|')
            let radioValue = ""
            let textValue = ""
            if (sIndex != -1) {
              radioValue = valueStr.substring(0, sIndex)
              textValue = valueStr.substring(sIndex + 1, valueStr.length)
            } else {
              radioValue = valueStr
            }
            form.itemRender[name] = {
              radioValue: radioValue,
              textValue: textValue,
              showText: false,
              textOptions: form.itemList[item].responseSet.options,
              checkShowText: function() {
                for (var i in this.textOptions) {
                  if (this.textOptions[i].value == this.radioValue && this.textOptions[i].hasText == "1") {
                    return this.showText = true
                  }
                }
                return this.showText = false
              },
              getData: function() {
                return this.radioValue + "|" + this.textValue
              }
            }
            form.itemRender[name].checkShowText()
          } else {
            form.itemRender[name] = form.itemData[name]
          }
        }
      }

      // custom表单的渲染
      for (var item in form.customList) {
        var name = form.customList[item]['name']
        if (name && form.customRender[name] !== undefined) {
          if (form.customList[item].responseType.id == "91") {
            let valueStr = form.customRender[name]
            let arrData = valueStr.split(",")
            form.customRender[name] = region.getRegionByCode(arrData[0], arrData[1], arrData[2])
            form.customRender[name]["text"] = arrData[3]
            // 图片组件
          } else if (form.customList[item].responseType.id == "9") {
            let valueStr = form.customRender[name] || ""
            if (!valueStr) {
              form.customRender[name] = []
            } else {
              let arrData = valueStr.split(",")
              for (var item in arrData) {
                arrData[item] = [util.baseUrl, arrData[item]]
              }
              form.customRender[name] = arrData
            }
            // 复选框组件
          } else if (form.customList[item].responseType.id == "3") {
            let valueStr = form.customRender[name] || ""
            if (!valueStr) {
              form.customRender[name] = {}
              for (var i in form.customList[item].responseSet.options) {
                form.customRender[name][form.customList[item].responseSet.options[i].value] = false;
              }
            } else {
              form.customRender[name] = {}
              for (var i in form.customList[item].responseSet.options) {
                if (valueStr.indexOf(form.customList[item].responseSet.options[i].value) != -1) {
                  form.customRender[name][form.customList[item].responseSet.options[i].value] = true;
                } else {
                  form.customRender[name][form.customList[item].responseSet.options[i].value] = false;
                }
              }
            }
            // 单选组件
          } else if (form.customList[item].responseType.id == "5") {
            let valueStr = form.customRender[name] || ""
            let sIndex = valueStr.indexOf('|')
            let radioValue = ""
            let textValue = ""
            if (sIndex != -1) {
              radioValue = valueStr.substring(0, sIndex)
              textValue = valueStr.substring(sIndex + 1, valueStr.length)
            } else {
              radioValue = valueStr
            }
            form.customRender[name] = {
              radioValue: radioValue,
              textValue: textValue,
              showText: false,
              textOptions: form.customList[item].options,
              checkShowText: function() {
                for (var i in this.textOptions) {
                  if (this.textOptions[i].value == this.radioValue && this.textOptions[i].hasText == "1") {
                    return this.showText = true
                  }
                }
                return this.showText = false
              },
              getData: function() {
                return this.radioValue + "|" + this.textValue
              }
            }
            form.customRender[name].checkShowText()
          } else {
            form.customRender[name] = form.customData[name]
          }
        }
      }
    }
    console.log(form)
  },

  // 获取到最新的Form表单数据
  getFormData: () => {
    return form.itemData
  },

  getFormCustomData: () => {
    return form.customData
  },

  // 根据name更新itemData或者customData
  updateData: (name, value) => {
    if (form.itemData[name] !== undefined) {
      form.itemData[name] = value
    }
    if (form.customData[name] !== undefined) {
      form.customData[name] = value
    }
  }
}

module.exports = form